#----------------------------------*-sh-*--------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | www.openfoam.com
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
#     Copyright (C) 2011-2016 OpenFOAM Foundation
#     Copyright (C) 2016-2020 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
#
# File
#     etc/config.csh/settings
#     - sourced by OpenFOAM-*/etc/cshrc
#
# Description
#     Settings for OpenFOAM
#
#------------------------------------------------------------------------------
setenv WM_ARCH `uname -s`                   # System name

set archOption=64
if ($?WM_ARCH_OPTION) then
    set archOption="$WM_ARCH_OPTION"
endif

unsetenv WM_ARCH_OPTION     # Compiling 32-bit on 64-bit system
setenv WM_COMPILER_ARCH     # Host compiler type (when different than target)
setenv WM_COMPILER_LIB_ARCH # Additional ending for lib directories
if (! $?WM_COMPILE_OPTION ) setenv WM_COMPILE_OPTION

# Adjust according to system and architecture
switch ($WM_ARCH)
case Linux:
    setenv WM_ARCH linux

    switch (`uname -m`)
    case i686:
        breaksw

    case x86_64:
        switch ("$archOption")
        case 32:
            setenv WM_ARCH_OPTION 32        # Need to propagate the value
            setenv WM_COMPILER_ARCH 64      # 64-bit compiler, 32-bit target
            breaksw

        case 64:
            setenv WM_ARCH linux64
            setenv WM_COMPILER_LIB_ARCH 64  # Target with lib64
            breaksw

        default:
            echo "Unknown WM_ARCH_OPTION '$archOption', should be 32|64"
            breaksw

        endsw
        breaksw

    case ia64:
        setenv WM_ARCH linuxIA64
        breaksw

    case armv7l:
        setenv WM_ARCH linuxARM7
        setenv WM_COMPILER_LIB_ARCH 32
        breaksw

    case aarch64:
        setenv WM_ARCH linuxARM64
        setenv WM_COMPILER_LIB_ARCH 64
        breaksw

    case ppc64:
        setenv WM_ARCH linuxPPC64
        setenv WM_COMPILER_LIB_ARCH 64
        breaksw

    case ppc64le:
        setenv WM_ARCH linuxPPC64le
        setenv WM_COMPILER_LIB_ARCH 64
        breaksw

    default:
        echo Unknown processor type `uname -m` for Linux
        breaksw

    endsw
    breaksw

case Darwin:    # Presume x86_64, with clang (not gcc) as system compiler
    setenv WM_ARCH darwin64
    if ("$WM_COMPILER" == Gcc) setenv WM_COMPILER Clang
    echo "Darwin support is clang/llvm only"
    breaksw

case SunOS*:
    setenv WM_ARCH solaris64
    setenv WM_COMPILER_LIB_ARCH 64
    breaksw

default:
    /bin/cat << UNSUPPORTED
===============================================================================
Operating system '$WM_ARCH' is unsupported by this OpenFOAM release.
For further assistance, please contact www.OpenFOAM.com
===============================================================================
UNSUPPORTED
    breaksw

endsw

#------------------------------------------------------------------------------

# wmake configuration
setenv WM_DIR "$WM_PROJECT_DIR/wmake"
setenv WM_LABEL_OPTION "Int$WM_LABEL_SIZE"
setenv WM_OPTIONS "$WM_ARCH$WM_COMPILER$WM_PRECISION_OPTION$WM_LABEL_OPTION$WM_COMPILE_OPTION"

# Base executables/libraries
setenv FOAM_APPBIN "$WM_PROJECT_DIR/platforms/$WM_OPTIONS/bin"
setenv FOAM_LIBBIN "$WM_PROJECT_DIR/platforms/$WM_OPTIONS/lib"

# Site-specific (group) files

# Default
set siteDir="$WM_PROJECT_DIR/site"

# User override
if ( $?WM_PROJECT_SITE ) then
    set siteDir="$WM_PROJECT_SITE"
else
    unsetenv WM_PROJECT_SITE
endif

# Shared site (group) executables/libraries
setenv FOAM_SITE_APPBIN "$siteDir/$FOAM_API/platforms/$WM_OPTIONS/bin"
setenv FOAM_SITE_LIBBIN "$siteDir/$FOAM_API/platforms/$WM_OPTIONS/lib"

# User executables/libraries
setenv FOAM_USER_APPBIN "$WM_PROJECT_USER_DIR/platforms/$WM_OPTIONS/bin"
setenv FOAM_USER_LIBBIN "$WM_PROJECT_USER_DIR/platforms/$WM_OPTIONS/lib"

# Prepend wmake to the path - not required for runtime-only environment
set foundDir="${WM_PROJECT_DIR}/wmake"
if ( $?WM_DIR ) then
    if ( -d "${WM_DIR}" ) set foundDir="${WM_DIR}"
endif
if ( -d "$foundDir" ) then
    setenv PATH "${foundDir}:${PATH}"
else
    unsetenv WM_DIR
endif


# Prepend OpenFOAM scripts to the path
setenv PATH "${WM_PROJECT_DIR}/bin:${PATH}"

# Prepend site-specific scripts to path - only if they exist
if ( -d "$siteDir/bin" ) then               # Generic
    _foamAddPath "$siteDir/bin"
endif
if ( -d "$siteDir/$FOAM_API/bin" ) then     # API-specific
    _foamAddPath "$siteDir/$FOAM_API/bin"
endif

# OpenFOAM executables (user, group, standard)
_foamAddPath "${FOAM_USER_APPBIN}:${FOAM_SITE_APPBIN}:${FOAM_APPBIN}"

# Dummy versions of external libraries. To be found last in library path
_foamAddLib  "$FOAM_LIBBIN/dummy"

# External (ThirdParty) libraries. Also allowed to be unset
if ( -d "$WM_THIRD_PARTY_DIR" ) then
    setenv FOAM_EXT_LIBBIN "$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER$WM_PRECISION_OPTION$WM_LABEL_OPTION/lib"
    _foamAddLib "$FOAM_EXT_LIBBIN"
else
    unsetenv FOAM_EXT_LIBBIN
endif

# OpenFOAM libraries (user, group, standard)
_foamAddLib  "${FOAM_USER_LIBBIN}:${FOAM_SITE_LIBBIN}:${FOAM_LIBBIN}"


#------------------------------------------------------------------------------

# Convenience
# ~~~~~~~~~~~

setenv FOAM_ETC "$WM_PROJECT_DIR/etc"
setenv FOAM_APP "$WM_PROJECT_DIR/applications"
setenv FOAM_SRC "$WM_PROJECT_DIR/src"
setenv FOAM_TUTORIALS "$WM_PROJECT_DIR/tutorials"
setenv FOAM_UTILITIES "$WM_PROJECT_DIR/applications/utilities"
setenv FOAM_SOLVERS "$WM_PROJECT_DIR/applications/solvers"
setenv FOAM_RUN "$WM_PROJECT_USER_DIR/run"

#------------------------------------------------------------------------------

# Compiler settings
# ~~~~~~~~~~~~~~~~~

# Default to system compiler
if (! $?WM_COMPILER_TYPE ) setenv WM_COMPILER_TYPE system

# Non-POSIX systems and cross-compiling
switch ("$WM_COMPILER")
case Mingw*:
    setenv WM_OSTYPE MSwindows
    breaksw
endsw

# Clear prior to sourcing
unset gcc_version gmp_version mpfr_version mpc_version clang_version
unsetenv GMP_ARCH_PATH MPFR_ARCH_PATH

# Load pre-defined compiler versions
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_foamEtc -config compiler

# ThirdParty base for compilers
set archDir="$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER_ARCH"

switch ("$WM_COMPILER_TYPE-$WM_COMPILER")
case ThirdParty-Gcc*:
    if (! $?gmp_version  ) set gmp_version=gmp-system
    if (! $?mpfr_version ) set mpfr_version=mpfr-system
    if (! $?mpc_version  ) set mpc_version=mpc-system
    set gccDir=$archDir/$gcc_version
    set gmpDir=$archDir/$gmp_version
    set mpfrDir=$archDir/$mpfr_version
    set mpcDir=$archDir/$mpc_version

    # Check that the compiler directory can be found
    if ( ! -d "$gccDir" ) then
        /bin/cat << GCC_NOT_FOUND
===============================================================================
Warning in $WM_PROJECT_DIR/etc/config.csh/settings:
Cannot find '$WM_COMPILER' compiler installation
    $gccDir

    Either install this compiler version, or use the system compiler by setting
    WM_COMPILER_TYPE to 'system' in \$WM_PROJECT_DIR/etc/cshrc.
===============================================================================
GCC_NOT_FOUND
    endif

    _foamAddMan     $gccDir/man
    _foamAddPath    $gccDir/bin

    # Add ThirdParty compiler libraries to run-time environment
    _foamAddLib     $gccDir/lib$WM_COMPILER_LIB_ARCH

    # Add gmp/mpfr/mpc libraries to run-time environment.
    # Require that they exist, automatically find lib64/ or lib/.
    if ( "${gmpDir:t}" != "gmp-system" ) then
        _foamAddLibAuto $gmpDir
        setenv GMP_ARCH_PATH $gmpDir    # For non-system CGAL
    endif
    if ( "${mpfrDir:t}" != "mpfr-system" ) then
        _foamAddLibAuto $mpfrDir
        setenv MPFR_ARCH_PATH $mpfrDir  # For non-system CGAL
    endif

    _foamAddLibAuto $mpcDir

    if ($?FOAM_VERBOSE && $?prompt) then
        echo "Using ThirdParty compiler"
        echo "    ${gccDir:t} (${gmpDir:t} ${mpfrDir:t} ${mpcDir:t})"
    endif
    breaksw

case ThirdParty-Clang*:
    set clangDir=$archDir/$clang_version

    # Check that the compiler directory can be found
    if ( ! -d "$clangDir" ) then
        /bin/cat << CLANG_NOT_FOUND
===============================================================================
Warning in $WM_PROJECT_DIR/etc/config.csh/settings:
Cannot find '$WM_COMPILER' compiler installation
    $clangDir

    Either install this compiler version, or use the system compiler by setting
    WM_COMPILER_TYPE to 'system' in \$WM_PROJECT_DIR/etc/cshrc.
===============================================================================
CLANG_NOT_FOUND
    endif

    _foamAddMan     $clangDir/man
    _foamAddPath    $clangDir/bin
    _foamAddLib     $clangDir/lib  # For things like libomp (openmp) etc

    if ($?FOAM_VERBOSE && $?prompt) then
        echo "Using ThirdParty compiler"
        echo "    ${clangDir:t}"
    endif
    breaksw

case -*:
case system-*:
case ThirdParty-*:
    # Using empty (system), system compiler or other ThirdParty compiler
    breaksw

default:
    /bin/cat << UNKNOWN_TYPE
===============================================================================
Unknown WM_COMPILER_TYPE="$WM_COMPILER_TYPE" - treating as 'system'
Please check your settings
===============================================================================
UNKNOWN_TYPE
    setenv WM_COMPILER_TYPE system
    breaksw

endsw

# Cleanup
# ~~~~~~~
unset archDir siteDir foundDir archOption
unset gcc_version gccDir
unset gmp_version gmpDir  mpfr_version mpfrDir  mpc_version mpcDir
unset clang_version clangDir
# Retain: _foamAddPath _foamAddLib _foamAddMan _foamAddLibAuto

#------------------------------------------------------------------------------
